package com.lobstar.xrpc.common.handler;

import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lobstar.xrpc.common.po.Const;
import com.lobstar.xrpc.common.po.SMessage;

import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;

public class SMessageEncoder extends MessageToMessageEncoder<SMessage>{

	public SMessageEncoder() {
		super();
	}


	private static ObjectMapper mapper = new ObjectMapper();
	@Override
	protected void encode(ChannelHandlerContext ctx, SMessage msg, List<Object> out) throws Exception {
		String string = mapper.writeValueAsString(msg)+Const.DELIMITER;
		out.add(ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(string), Charset.forName("utf-8")));
	}

}
